package com.cloudkinto.common.utils;

import com.cloudkinto.common.i18n.ChineseMessageFactory;
import com.cloudkinto.common.i18n.EnglishMessageFactory;
import com.cloudkinto.common.i18n.JapaneseMessageFactory;
import com.cloudkinto.common.i18n.Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Author Administrator
 *
 * @Des
 * @Version
 * @Date 2020/5/29
 */
public class StringsUtil {
    public static String filterChinese(String input) {
       return filterChinese(input, "");
    }


    public static String filterChinese(String input, String replacement) {
        // 使用正则表达式匹配汉字
        String regex = "[\u4E00-\u9FA5]";
        // 使用正则表达式过滤出汉字
        String filtered = input.replaceAll(regex, replacement);
        return filtered;
    }

    public static String subStringByUnic(String s, int length) throws Exception {

        byte[] bytes = s.getBytes("Unicode");
        int n = 0; // 表示当前的字节数
        int i = 2; // 要截取的字节数，从第3个字节开始
        for (; i < bytes.length && n < length; i++) {
            // 奇数位置，如3、5、7等，为UCS2编码中两个字节的第二个字节
            if (i % 2 == 1) {
                n++; // 在UCS2第二个字节时n加1
            } else {
                // 当UCS2编码的第一个字节不等于0时，该UCS2字符为汉字，一个汉字算两个字节
                if (bytes[i] != 0) {
                    n++;
                }
            }
        }
        // 如果i为奇数时，处理成偶数
        if (i % 2 == 1) {
            // 该UCS2字符是汉字时，去掉这个截一半的汉字
            if (bytes[i - 1] != 0)
                i = i - 1;
                // 该UCS2字符是字母或数字，则保留该字符
            else
                i = i + 1;
        }
        return new String(bytes, 0, i, "Unicode");
    }

    public static String getJpAreaByEnglishCode(String code) {
        switch (code.replaceAll("ō", "o")
                .replaceAll("Ō", "o")
                .replaceAll("-ken", "")
                .replaceAll("-to", "")
                .replaceAll("-fu", "")
                .toLowerCase()) {
            case "hokkaido":
                return "北海道";
            case "aomori":
                return "青森県";
            case "iwate":
                return "岩手県";
            case "miyagi":
                return "宮城県";
            case "akita":
                return "秋田県";
            case "yamagata":
                return "山形県";
            case "ibaraki":
                return "茨城県";
            case "fukushima":
                return "福島県";
            case "tochigi":
                return "栃木県";
            case "gunma":
                return "群馬県";
            case "saitama":
                return "埼玉県";
            case "chiba":
                return "千葉県";
            case "tokyo":
                return "東京都";
            case "kanagawa":
                return "神奈川県";
            case "niigata":
                return "新潟県";
            case "toyama":
                return "富山県";
            case "ishikawa":
                return "石川県";
            case "fukui":
                return "福井県";
            case "yamanashi":
                return "山梨県";
            case "nagano":
                return "長野県";
            case "gifu":
                return "岐阜県";
            case "shizuoka":
                return "静岡県";
            case "aichi":
                return "愛知県";
            case "mie":
                return "三重県";
            case "shiga":
                return "滋賀県";
            case "kyoto":
                return "京都府";
            case "osaka":
                return "大阪府";
            case "hyogo":
                return "兵庫県";
            case "nara":
                return "奈良県";
            case "wakayama":
                return "和歌山県";
            case "tottori":
                return "鳥取県";
            case "shimane":
                return "島根県";
            case "okayama":
                return "岡山県";
            case "hiroshima":
                return "広島県";
            case "yamaguchi":
                return "山口県";
            case "tokushima":
                return "徳島県";
            case "kagawa":
                return "香川県";
            case "ehime":
                return "愛媛県";
            case "kochi":
                return "高知県";
            case "fukuoka":
                return "福岡県";
            case "saga":
                return "佐賀県";
            case "nagasaki":
                return "長崎県";
            case "kumamoto":
                return "熊本県";
            case "oita":
                return "大分県";
            case "miyazaki":
                return "宮崎県";
            case "kagoshima":
                return "鹿児島県";
            case "okinawa":
                return "沖縄県";
            default:
                return code;
        }
    }

    public static String createI18Message(String key, Object... args) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String i18n = "ch";
        if (servletRequestAttributes != null) {
            i18n= servletRequestAttributes.getRequest().getHeader("lang");
        }
        String message = key;
        if ("en".equals(i18n)) {
            EnglishMessageFactory factory = new EnglishMessageFactory();
            message = factory.getMessageString(message, args);
        } else if ("jp".equals(i18n)) {
            JapaneseMessageFactory factory = new JapaneseMessageFactory();
            message = factory.getMessageString(message, args);
        } else {
            ChineseMessageFactory factory = new ChineseMessageFactory();
            message = factory.getMessageString(message, args);
        }
        return message;
    }
    public static String createI18Message(Message messageBean) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String i18n = "ch";
        if (servletRequestAttributes != null) {
            i18n= servletRequestAttributes.getRequest().getHeader("lang");
        }
        String message = messageBean.getMessage();
        if ("en".equals(i18n)) {
            EnglishMessageFactory factory = new EnglishMessageFactory();
            message = factory.getMessageString(messageBean.getMessageKey(), messageBean.getArgs());
        } else if ("jp".equals(i18n)) {
            JapaneseMessageFactory factory = new JapaneseMessageFactory();
            message = factory.getMessageString(messageBean.getMessageKey(), messageBean.getArgs());
        } else {
            ChineseMessageFactory factory = new ChineseMessageFactory();
            message = factory.getMessageString(messageBean.getMessageKey(), messageBean.getArgs());
        }
        return message;
    }

    public static String checkPostCode(String postCode) {
        if (StringUtils.isBlank(postCode)) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < postCode.length(); i++) {
            String c = postCode.charAt(i) + "";
            if (!c.matches("[0-9０-９-]+")) {
                continue;
            }
            if (c.matches("[０-９]")) {
                builder.append(Integer.parseInt(c));
            } else {
                builder.append(c);
            }
        }
        return builder.toString();
    }
}
